<?php
/**
 * Created by 角色类.
 * User: 尚金殿
 * Date: 2018/8/22
 * Time: 11:07
 */

namespace app\admin\controller;


use think\Db;

class GroupController extends CommonController
{
    protected $mysql = 'group';   //表名
    protected $pk = 'group_id';   //主键
    protected $name = 'group_name';  //模糊匹配
    protected $status = 'group_status';       //状态名




    //角色添加
    public function add(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();

            if(!empty($data['group_id'])){
                $this->save($data);
            }

            //判定数据
            if(!$data['group_name'] || empty($data['group_name'])){
                return show(0,'角色名称不能为空');
            }

            if(!$data['rule_id']){
                return show(0,'权限不能为空');
            }



            //转化数据
            $data['rule_id'] = implode(',',$data['rule_id']);

            $res = $this->getAdd($data);

            if(!$res){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            //获取全部权限
            $rule = Db::name('rule')->where('rule_status','>',-1)->select();
            //树形结构
            $rule = getTree($rule);
            //分配变量
            $this->assign('rule',$rule);
            //加载模版
            return $this->fetch();
        }
    }


    //角色编辑
    public function edit(){
        //获取ID
        $id = $this->request->param('id');
        //获取全部权限
        $rule = Db::name('rule')->where('rule_status','>',-1)->select();
        $rule = getTree($rule);

        //获取当前角色信息
        $group = Db::name($this->mysql)->where($this->pk,$id)->find();
        $group['rule_id'] = explode(',',$group['rule_id']);

        //变量输出
        $this->assign(['rule'=>$rule,'group'=>$group]);

        //加载模版
        return $this->fetch();

    }








}